import { BaseWindow } from "../../../../component/window/BaseWindow";
import { NetDispatcher } from "../../../../mvc/core/NetDispatcher";
import { ServerCommand } from "../../../mvc/ServerCommand";
import { ObjCreate } from "../../common/ObjCreate";
import { GlobalStyle } from "../../../../common/global/GlobalStyle";
import { NumInput } from "../../common/NumInput";
import { GTextFormat } from "../../../../common/GTextFormat";
import { FontUtil } from "../../../../common/font/FontUtil";
import { GButton } from "../../../../../com/mui/controls/GButton";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../events/DataEvent";
import { EventName } from "../../../mvc/EventName";
import { SGuildShopItem } from "../../../../../Message/Game/SGuildShopItem";
import { ItemData } from "../../../resource/info/ItemData";
import { ColorConfig } from "../../../resource/ColorConfig";
import { HTMLUtil } from "../../../../../com/gengine/utils/HTMLUtil";
import { Cache } from "../../../cache/Cache";
import { SPlayerGuildInfo } from "../../../../../Message/Game/SPlayerGuildInfo";
import { TItem } from "../../../../../Message/Db/Tables/TItem";
import { ColorInfo } from "../../../resource/info/ColorInfo";
import { _amount } from "../../../../../this/_amount";
type int = number;
//class GuildShopBuyWindow
    
    export  class GuildShopBuyWindow extends BaseWindow
    {
        constructor()
        {
            super();
            this.init();
            NetDispatcher.addCmdListener(ServerCommand.GuildUpdateInfo, this.onGuildUpdateInfoHandler);
            return;
        }

        public init(): void
        {
            this.setSize(258, 200);
            this.titleHeight = 28;
            ObjCreate.createBg(20, 42, 218, 142, this);
            this._tileTxt = ObjCreate.createTextField("", 30, 12, 170, 20, this, GlobalStyle.dialogTitle);
            this.title = this._tileTxt;
            ObjCreate.createTextField("购买数量", 28, 45, 200, 20, this);
            this._numStepper = new NumInput();
            this._numStepper.maxNum = 99;
            this._numStepper.x = 80;
            this._numStepper.y = 45;
            this._numStepper.addEventListener(flash.events.Event.CHANGE, this.onChangeHandler);
            this.addChild(this._numStepper);
            ObjCreate.createTextField("商品单价：", 28, 68, 200, 20, this);
            ObjCreate.createTextField("贡献", 100, 68, 200, 20, this);
            this._priceTxt = ObjCreate.createTextField("", 125, 68, 63, 20, this);
            ObjCreate.createTextField("总花费：", 28, 91, 200, 20, this);
            ObjCreate.createTextField("贡献", 100, 91, 200, 20, this);
            this._totalPriceTxt = ObjCreate.createTextField("", 125, 92, 63, 20, this);
            this._tipText = ObjCreate.createTextField("你的仙盟贡献不足！", 60, 120, 220, 20, this, new GTextFormat(FontUtil.songtiName, 12, 16711680));
            this._tipText.visible = false;
            this._okBtn = new GButton();
            this._okBtn.label = "确认";
            this._okBtn.name = "SaleButton";
            this._okBtn.styleName = "Button";
            this._okBtn.setSize(48, 27);
            this._okBtn.move(44, 151);
            this._okBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onOkBtnClickHandler);
            this.addChild(this._okBtn);
            this._cancelBtn = new GButton();
            this._cancelBtn.label = "取消";
            this._cancelBtn.name = "SaleButton";
            this._cancelBtn.styleName = "Button";
            this._cancelBtn.setSize(48, 27);
            this._cancelBtn.move(140, 151);
            this._cancelBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onCancelBtnClickHandler);
            this.addChild(this._cancelBtn);
            return;
        }

        /* internal  */onOkBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this._shopItem) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.GuildBuyShopItem, {"itemCode": this._shopItem.itemCode, "amount": _amount}));
                this.onCancelBtnClickHandler(null);
            }
            return;
        }

        /* internal  */onCancelBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            hide();
            return;
        }

        public onChangeHandler(arg1: Object /* flash.events.Event */): void
        {
            this._amount = this._numStepper.currentNum;
            this._totalPrice = this._unitPrice * this._amount;
            this._totalPriceTxt.text = this._totalPrice.toString();
            this.checkMoney();
            return;
        }

        public updateShopItemInfo(arg1: SGuildShopItem): void
        {
            var loc1=null;
            if (arg1) 
            {
                this._shopItem = arg1;
                loc1 = new ItemData(this._shopItem.itemCode);
                this._item = loc1.itemInfo.item;
                this._colorInfo = ColorConfig.instance.getItemColor(this._item.color);
                if (this._colorInfo) 
                {
                    this._colorName = this._colorInfo.color;
                }
                this._nameStr = HTMLUtil.addColor(this._item.name, this._colorName);
                this._tileTxt.htmlText = this._nameStr;
                this._numStepper.maxNum = arg1.buyLimit;
                this._numStepper.currentNum = 1;
                this._amount = this._numStepper.currentNum;
                this._unitPrice = this._shopItem.price;
                this._totalPrice = this._unitPrice * this._amount;
                this._priceTxt.text = this._unitPrice.toString();
                this._totalPriceTxt.text = this._totalPrice.toString();
                this.checkMoney();
            }
            return;
        }

        /* internal  */checkMoney(): void
        {
            var loc1=Cache.instance.guild.myGuildInfo;
            if (loc1) 
            {
                if (loc1.contribution >= this._totalPrice) 
                {
                    this._tipText.visible = false;
                    this._okBtn.enabled = true;
                }
                else 
                {
                    this._tipText.visible = true;
                    this._okBtn.enabled = false;
                }
            }
            else 
            {
                this.hide();
            }
            return;
        }

        /* internal  */onGuildUpdateInfoHandler(arg1: SPlayerGuildInfo=null): void
        {
            this.checkMoney();
            return;
        }

        /* internal */ /* var */_tileTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_priceTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_totalPriceTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_numStepper: NumInput;

        /* internal */ /* var */_okBtn: GButton;

        /* internal */ /* var */_cancelBtn: GButton;

        /* internal */ /* var */_windowCenterB: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_tipText: Object /* flash.text.TextField */;

        /* internal */ /* var */_shopItem: SGuildShopItem;

        /* internal */ /* var */_totalPrice: number=0;

        /* internal */ /* var */_amount: int=1;

        /* internal */ /* var */_itemInfo: TItem;

        /* internal */ /* var */_unitPrice: int;

        /* internal */ /* var */_item: TItem;

        /* internal */ /* var */_colorInfo: ColorInfo;

        /* internal */ /* var */_colorName: string;

        /* internal */ /* var */_nameStr: string;
    }
